#include "stdafx.h"

#include "..\inc\Page.h"
#include "..\inc\PageManager.h"
#include "..\inc\PageStrategy.h"
#include "..\inc\PageWorld.h"
#include "..\inc\PageWorldSection.h"

using namespace PageSystem;

PagedWorld::PagedWorld(const String& name, PageManager* manager)
	:mName(name),mManager(manager),mPageProvider(NULL)
{
}

PagedWorld::~PagedWorld()
{
}

PagedWorldSection* PagedWorld::getSection(const String& name)
{
	SectionMap::iterator pos = mSections.find( name );
	if( pos == mSections.end() )
		return NULL;

	return pos->second;
}

PagedWorldSection* PagedWorld::createSection(const String& strategyName, SceneManager* sceneMgr, const String& sectionName )
{
	PagedWorldSection* pWorldSection = NULL;
	pWorldSection = this->getSection( sectionName );
	if( pWorldSection != NULL )
		return pWorldSection;

	pWorldSection = mManager->createWorldSection( strategyName , sectionName , this , sceneMgr );
	return pWorldSection;
}

PagedWorldSection* PagedWorld::createSection(SceneManager* sceneMgr,const String& typeName,const String& sectionName )
{
	PagedWorldSection* pWorldSection = NULL;
	pWorldSection = this->getSection( sectionName );
	if( pWorldSection != NULL )
		return pWorldSection;

	pWorldSection = mManager->createWorldSection( typeName , sectionName , this , sceneMgr );
	return pWorldSection;
}

PagedWorldSection* PagedWorld::createSection(PageStrategy* strategy, SceneManager* sceneMgr, const String& sectionName )
{
	PagedWorldSection* pWorldSection = NULL;
	pWorldSection = this->getSection( sectionName );
	if( pWorldSection != NULL )
		return pWorldSection;

	pWorldSection = mManager->createWorldSection( strategy->getName() , sectionName , this , sceneMgr );
	return pWorldSection;
}

/** Destroy a section of world. */
void PagedWorld::destroySection(const String& name)
{
	SectionMap::iterator pos = mSections.find( name );
	if( pos == mSections.end() )
		return;
	
	delete pos->second;
	mSections.erase(pos);
}
/** Destroy a section of world. */
void PagedWorld::destroySection(PagedWorldSection* sec)
{
	if( sec == NULL )
		return;

	this->destroySection( sec->getName() );
}
/** Destroy all world sections */
void PagedWorld::destroyAllSections()
{
	SectionMap::iterator pos = mSections.begin();
	for( ; pos != mSections.end() ; ++pos )
		delete pos->second;

	mSections.clear();
}

std::ostream& operator <<( std::ostream& o, const PagedWorld& p )
{
	return o;
}